﻿#include "mainwindow.h"
#include "cell.h"
#include <QMenuBar>
#include <QHBoxLayout>
#include <QCloseEvent>
#include <QMessageBox>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(QString::fromUtf8("Ô Chử Siêu Cấp by Nguyễn Văn Cao Nguyên - HocVui.Net"));
    edit = new EditScreen();
    play = new PlayScreen();
    QWidget* mainwidget = new QWidget(this);
    QHBoxLayout* mainlayout = new QHBoxLayout(mainwidget);
    mainlayout->setAlignment(Qt::AlignTop);
    mainlayout->addWidget(edit);
    mainlayout->addWidget(play);

    //QLabel* hellolabel = new QLabel(QString::fromUtf8("Mọi chi tiết xin liên hệ <a href=\"mailto:nvcnvn@hocvui.net\">nvcnvn@hocvui.net</a>\n<b>Hướng dẫn sử dụng</b>"));
    //mainlayout->addWidget(hellolabel);
    mainwidget->setLayout(mainlayout);
    edit->hide();
    play->hide();
    setMinimumWidth(900);
    setMinimumHeight(600);
    QMenu* fileMenu = menuBar()->addMenu(QString::fromUtf8("Chơi"));

    QAction* newGameAction = fileMenu->addAction(QString::fromUtf8("Bắt đầu"), this, SLOT(newGame()));
    newGameAction->setShortcut(QKeySequence::New);

    QAction* loadGameAction = fileMenu->addAction(QString::fromUtf8("Tải bản lưu"), this, SLOT(loadGame()) );
    loadGameAction->setShortcut( QKeySequence::Open );

    fileMenu = menuBar()->addMenu(QString::fromUtf8("Soạn thảo"));
    QAction* newMapAction = fileMenu->addAction(QString::fromUtf8("Tạo mới"), this, SLOT(newGrid()));
    newMapAction->setShortcut(QKeySequence(tr("Ctrl+Shift+N")));

    QAction* loadMapAction = fileMenu->addAction(QString::fromUtf8("Sửa"), this, SLOT(loadGrid()));
    loadMapAction->setShortcut( QKeySequence(tr("Ctrl+Shift+O")) );

    setCentralWidget(mainwidget);
}

void MainWindow::newGame()
{
    edit->hide();
    play->show();
    play->ShowSelectWindow(true);
}

void MainWindow::loadGame()
{
    edit->hide();
    play->show();
    play->ShowSelectWindow(false);
}

void MainWindow::newGrid()
{
    if(play->grid->isPlaying)
    {
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(QString::fromUtf8("Vẫn đang chơi, chơi cho xong hay save lại rồi làm gì thi làm."));
        msgBox.exec();
        return;
    }

    play->hide();
    edit->show();
    edit->grid->Clean();
    edit->grid->R = 0;
    edit->grid->G = 0;
    edit->grid->B = 0;
}

void MainWindow::loadGrid()
{
    if(play->grid->isPlaying)
    {
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(QString::fromUtf8("Vẫn đang chơi, chơi cho xong hay save lại rồi làm gì thi làm."));
        msgBox.exec();
        return;
    }
    play->hide();
    edit->show();
    edit->grid->Load();
}

void MainWindow::closeEvent(QCloseEvent * e)
{
    if(!play->isHidden() && play->grid->isPlaying)
    {
        if(!play->grid->Done(true))
        {
            e->ignore();
            return;
        }
    }
    e->accept();
}

MainWindow::~MainWindow()
{

}
